package com.allo2o.common.biz.sequence.longimpl;

import java.util.concurrent.atomic.AtomicLong;

import com.allo2o.common.biz.sequence.SequenceRange;

public class LongSequenceRange implements SequenceRange<Long> {

    /**
     * 
     */
    private static final long serialVersionUID = -1582713743711325614L;

    private final Long        min;

    private final Long        max;

    private final AtomicLong  value;

    private volatile boolean  over             = false;

    public LongSequenceRange(Long min, Long max) {
        this.min = min;
        this.max = max;
        this.value = new AtomicLong(min);
    }

    public Long getAndIncrement() {
        Long currentValue = value.getAndIncrement();
        if (currentValue.longValue() > max.longValue()) {
            over = true;
            return -1L;
        }
        return currentValue;
    }

    @Override
    public Long getMax() {
        return max;
    }

    @Override
    public Long getMin() {
        return min;
    }

    @Override
    public boolean isOver() {
        return over;
    }

}
